<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linux文件权限管理：如何正确设置文件和目录权限</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1a202c;
            color: #e2e8f0;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
        }
        .permission-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 6px;
            font-weight: 600;
            display: inline-block;
            margin: 0.25rem;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e2e8f0, transparent);
            margin: 3rem 0;
        }
        .first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                Linux文件权限管理
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                如何正确设置文件和目录权限
            </p>
            <div class="flex justify-center space-x-4 text-lg">
                <span class="flex items-center"><i class="fas fa-shield-alt mr-2"></i>系统安全</span>
                <span class="flex items-center"><i class="fas fa-lock mr-2"></i>数据保护</span>
                <span class="flex items-center"><i class="fas fa-user-shield mr-2"></i>权限管理</span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-5xl">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-2xl p-8 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="first-letter">在</span>Linux操作系统中，文件和目录权限的正确设置是确保系统安全性和数据保密性的重要一环。不正确的权限设置可能导致敏感数据泄露、恶意操作和系统崩溃。本文将深入探讨如何正确设置文件和目录权限，以及通过实际代码示例来演示权限管理的方法。
            </p>
        </div>

        <!-- Permission Visualization -->
        <div class="bg-white rounded-2xl shadow-2xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-diagram-project mr-3 text-purple-600"></i>权限体系结构
            </h2>
            <div class="mermaid">
                graph TD
                    A[Linux文件权限系统] --> B[基本权限]
                    A --> C[特殊权限]
                    B --> D[读权限 r=4]
                    B --> E[写权限 w=2]
                    B --> F[执行权限 x=1]
                    C --> G[SUID]
                    C --> H[SGID]
                    C --> I[Sticky Bit]
                    D --> J[查看文件内容]
                    E --> K[修改文件内容]
                    F --> L[运行程序/进入目录]
            </div>
        </div>

        <!-- Section 1: Permission Basics -->
        <div class="bg-white rounded-2xl shadow-2xl p-8 mb-12 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-key mr-3 text-indigo-600"></i>权限基础
            </h2>
            <p class="text-lg mb-6 text-gray-700">
                在Linux中，每个文件和目录都有三组权限：文件所有者、同组用户和其他用户。每组权限分为读（r）、写（w）和执行（x）。
            </p>
            <div class="bg-gray-50 rounded-lg p-6 mb-6">
                <p class="text-lg mb-4">例如，权限字符串 <code class="permission-box">-rw-r--r--</code> 表示：</p>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div class="bg-indigo-100 rounded-lg p-4 text-center">
                        <i class="fas fa-user text-3xl text-indigo-600 mb-2"></i>
                        <h4 class="font-bold text-indigo-800">文件所有者</h4>
                        <p class="text-indigo-700">读写权限 (rw-)</p>
                    </div>
                    <div class="bg-purple-100 rounded-lg p-4 text-center">
                        <i class="fas fa-users text-3xl text-purple-600 mb-2"></i>
                        <h4 class="font-bold text-purple-800">同组用户</h4>
                        <p class="text-purple-700">只读权限 (r--)</p>
                    </div>
                    <div class="bg-pink-100 rounded-lg p-4 text-center">
                        <i class="fas fa-globe text-3xl text-pink-600 mb-2"></i>
                        <h4 class="font-bold text-pink-800">其他用户</h4>
                        <p class="text-pink-700">只读权限 (r--)</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Section 2: Numeric Notation -->
        <div class="bg-white rounded-2xl shadow-2xl p-8 mb-12 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-calculator mr-3 text-green-600"></i>使用数字表示法
            </h2>
            <p class="text-lg mb-6 text-gray-700">Linux使用数字来表示权限，通过组合这些数字可以快速设置权限。</p>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
                <div class="bg-gradient-to-br from-blue-500 to-blue-600 text-white rounded-lg p-6 text-center card-hover">
                    <i class="fas fa-eye text-4xl mb-3"></i>
                    <h3 class="text-2xl font-bold mb-2">读权限</h3>
                    <p class="text-4xl font-bold">4</p>
                </div>
                <div class="bg-gradient-to-br from-green-500 to-green-600 text-white rounded-lg p-6 text-center card-hover">
                    <i class="fas fa-pen text-4xl mb-3"></i>
                    <h3 class="text-2xl font-bold mb-2">写权限</h3>
                    <p class="text-4xl font-bold">2</p>
                </div>
                <div class="bg-gradient-to-br from-purple-500 to-purple-600 text-white rounded-lg p-6 text-center card-hover">
                    <i class="fas fa-play text-4xl mb-3"></i>
                    <h3 class="text-2xl font-bold mb-2">执行权限</h3>
                    <p class="text-4xl font-bold">1</p>
                </div>
            </div>

            <div class="bg-gray-900 rounded-lg p-6">
                <p class="text-white mb-3">示例：设置文件权限为644</p>
                <code class="text-green-400 text-lg">chmod 644 file.txt</code>
                <p class="text-gray-400 mt-3">文件所有者：6 (4+2) = 读+写 | 同组用户：4 = 读 | 其他用户：4 = 读</p>
            </div>
        </div>

        <!-- Section 3: Common Commands -->
        <div class="bg-white rounded-2xl shadow-2xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-terminal mr-3 text-red-600"></i>常用命令示例
            </h2>

            <div class="space-y-8">
                <div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-700 flex items-center">
                        <i class="fas fa-edit mr-2 text-orange-500"></i>改变文件权限
                    </h3>
                    <p class="text-lg mb-4 text-gray-600">使用 <code class="permission-box bg-gray-800">chmod</code> 命令可以改变文件和目录的权限。</p>
                    <div class="code-block">
                        <pre>chmod +x script.sh    # 添加执行权限
chmod 755 directory   # 设置目录权限为755
chmod -w file.txt     # 移除写权限</pre>
                    </div>
                </div>

                <div class="section-divider"></div>

                <div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-700 flex items-center">
                        <i class="fas fa-user-cog mr-2 text-blue-500"></i>改变所有者和所属组
                    </h3>
                    <p class="text-lg mb-4 text-gray-600">使用 <code class="permission-box bg-gray-800">chown</code> 和 <code class="permission-box bg-gray-800">chgrp</code> 命令管理文件归属。</p>
                    <div class="code-block">
                        <pre>chown user1:group1 file.txt   # 同时改变所有者和组
chown user2 file.txt          # 只改变所有者
chgrp group2 file.txt         # 只改变所属组</pre>
                    </div>
                </div>
            </div>
        </div>

        <!-- Section 4: Special Permissions -->
        <div class="bg-white rounded-2xl shadow-2xl p-8 mb-12 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-star mr-3 text-yellow-500"></i>特殊权限标志
            </h2>
            <p class="text-lg mb-6 text-gray-700">除了基本权限外，Linux还有一些特殊权限标志，用于实现更高级的权限控制。</p>

            <div class="bg-gradient-to-r from-yellow-50 to-orange-50 rounded-lg p-6 mb-6">
                <h3 class="text-xl font-bold mb-3 text-gray-800 flex items-center">
                    <i class="fas fa-user-shield mr-2 text-orange-600"></i>SUID（Set User ID）
                </h3>
                <p class="text-gray-700 mb-3">设置SUID标志后，用户可以以文件所有者的权限执行可执行文件。</p>
                <div class="code-block">
                    <pre>chmod +s executable_file   # 设置SUID
chmod 4755 program        # 使用数字表示法设置SUID</pre>
                </div>
            </div>
        </div>

        <!-- Section 5: Practical Examples -->
        <div class="bg-white rounded-2xl shadow-2xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-laptop-code mr-3 text-teal-600"></i>实际场景示例
            </h2>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="bg-blue-50 rounded-lg p-6 card-hover">
                    <h3 class="text-xl font-bold mb-3 text-blue-800 flex items-center">
                        <i class="fas fa-server mr-2"></i>Web服务器文件权限
                    </h3>
                    <p class="text-gray-700 mb-3">在Web服务器上